<?php

namespace app\api\controller;

use app\api\model\Member;
use think\Model;
use function Sodium\add;

class Base
{
    public function register()
    {
        //第一步：接收必要的参数，并完成参数的校验
        $mobile = input('get.mobile');
        $password = input('get.password');
        $nickname = input('get.nickname');
        if (empty($mobile)) output_error('手机号不能为空');
        if (empty($password)) output_error('密码不能为空');
        if (empty($nickname)) output_error('昵称不能为空');
        if (strlen($mobile) != 11) output_error('手机号码格式不正确');
        //第二步：在数据中查找 该手机号是否在存在， 如果已经存在了，就直接返回 该手机号已被注册
        if (Member::register($mobile, $password, $nickname)) output_data('注册成功');
        output_error('注册失败，请检查提交的数据');
    }


    public function login(){
        $mobile = input('get.mobile');
        $password = input('get.password');
        if (empty($mobile)) output_error('手机号不能为空');
        if (empty($password)) output_error('密码不能为空');
        $condition = array();
        $condition[]=['member_mobile','=', $mobile];
        $condition =['member_password','=',md5($password)];
        $member = Member::where($condition)->find();
        if (empty($member)) output_error( msg: '账号或者密码错误');
        $token = md5(string: $member['member_id'] . time());
        $data = array();
        $data['member_id'] = $member['member_id'];
        $data['token_content'] = $token;
        $data['device_model'] = $model;
        $data['end_time'] = time() + 65535;
        $data['add_time'] = time();
        MemberToken::insert($data);
        output_data($token);



    }
}

